package com.web;

import com.google.gson.Gson;
import com.pojo.Book;
import com.pojo.Cart;
import com.pojo.CartItem;
import com.service.BookService;
import com.service.impl.BookServiceImpl;
import com.uitl.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CartServlet extends BaseServlet{
    //加入购物车
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BookService bookService = new BookServiceImpl();
//        System.out.println("加入购物车");
//        System.out.println("商品编号："+req.getParameter("id"));
        //获取请求的参数，商品编号
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        //调用bookService.queryBookById(id)得到图书的信息
        Book book = bookService.queryBookById(id);
        //把图书信息转化成为CartItem商品项
        CartItem cartItem= new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //调用Cart.addItem(CartItem);添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");//购物车只有一个，不能重复创建，否则每次添加购物车内只有一项
        if (cart==null){
             cart = new Cart();
             req.getSession().setAttribute("cart",cart);
        }

        cart.addItem(cartItem);
        System.out.println(cart);

        //最后一个添加的商品名称
        req.getSession().setAttribute("lastName",cartItem.getName());

        //重定向回【原来】商品列表的页面
        resp.sendRedirect(req.getHeader("Referer"));
        //请求头Referer可以返回当前页面的地址栏的地址
    }

    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BookService bookService = new BookServiceImpl();
//        System.out.println("加入购物车");
//        System.out.println("商品编号："+req.getParameter("id"));
        //获取请求的参数，商品编号
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        //调用bookService.queryBookById(id)得到图书的信息
        Book book = bookService.queryBookById(id);
        //把图书信息转化成为CartItem商品项
        CartItem cartItem= new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //调用Cart.addItem(CartItem);添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");//购物车只有一个，不能重复创建，否则每次添加购物车内只有一项
        if (cart==null){
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }

        cart.addItem(cartItem);
        System.out.println(cart);

        //最后一个添加的商品名称
        req.getSession().setAttribute("lastName",cartItem.getName());

        //返回购物车总的商品数量和最后一个添加的商品名称
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("totalCount",cart.getTotalCount());
        resultMap.put("lastName",cartItem.getName());
        Gson gson = new Gson();
        String resultMapJsonString = gson.toJson(resultMap);
        resp.getWriter().write(resultMapJsonString);
    }

    //删除购物车中的商品
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取传入的参数id
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        //获取Session域当中的购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null){
            //删除购物车商品项
            cart.deleteItem(id);
            //重定向回原来的页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    //清空购物车
    protected void  clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart !=null){
            //若购物车非空，则清空购物车
            cart.clear();
            //重定向回原来的页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    //修改商品数量
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数：商品编号、商品数量
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        int count = WebUtils.parseInt(req.getParameter("count"),1);
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart!=null){
            //修改商品数量
            cart.updateCount(id,count);
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
}
